<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>选择 MySQL 数据库</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.mysql-select-db" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_select_db</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_select_db</span> &mdash; <span class="dc-title">选择 MySQL 数据库</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.mysql-select-db-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>mysql_select_db</strong></span>
       ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$database_name</code></span>
      [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$
        link_identifier
       </code></span>
     ] ) : <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
    </p>
    <p class="para">
     <span class="function"><strong>mysql_select_db()</strong></span>
     设定与指定的连接标识符所关联的服务器上的当前激活数据库。如果没有指定连接标识符，则使用上一个打开的连接。如果没有打开的连接，本函数将无参数调用
     <span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span> 来尝试打开一个并使用之。
    </p>
    <p class="para">
     每个其后的
     <span class="function"><a href="mysql_query.html" class="function">mysql_query()</a></span>
     调用都会作用于活动数据库。
    </p>
    <p class="para">
     <div class="example" id="example-2167">
      <p><strong>Example #1 <span class="function"><strong>mysql_select_db()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$lnk&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">'Not&nbsp;connected&nbsp;:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">//&nbsp;make&nbsp;foo&nbsp;the&nbsp;current&nbsp;db<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$lnk</span><span style="color: #007700">)&nbsp;or&nbsp;die&nbsp;(</span><span style="color: #DD0000">'Can\'t&nbsp;use&nbsp;foo&nbsp;:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     参见
     <span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span>，<span class="function"><a href="mysql_pconnect.html" class="function">mysql_pconnect()</a></span> 和
     <span class="function"><a href="mysql_query.html" class="function">mysql_query()</a></span>。
    </p>
    <p class="para">
     为向下兼容仍然可以使用
     <span class="function"><strong>mysql_selectdb()</strong></span>，但反对这样做。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-select-db-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">database_name</code></dt>

     <dd>

      <p class="para">
       The name of the database that is to be selected.
      </p>
     </dd>

    
    <dt>
<code class="parameter">
link_identifier</code></dt>
<dd>
<p class="para">MySQL
连接。如不指定连接标识，则使用由 <span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span>
最近打开的连接。如果没有找到该连接，会尝试不带参数调用
<span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span>
来创建。如没有找到连接或无法建立连接，则会生成
<strong><code>E_WARNING</code></strong> 级别的错误。</p></dd>


   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-select-db-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-select-db-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2168">
    <p><strong>Example #2 <span class="function"><strong>mysql_select_db()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Not&nbsp;connected&nbsp;:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;make&nbsp;foo&nbsp;the&nbsp;current&nbsp;db<br /></span><span style="color: #0000BB">$db_selected&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$db_selected</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die&nbsp;(</span><span style="color: #DD0000">'Can\'t&nbsp;use&nbsp;foo&nbsp;:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-select-db-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    为了向下兼容，可以使用下列已废弃的别名：
    <span class="function"><strong>mysql_selectdb()</strong></span>
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-select-db-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysql_connect.html" class="function" rel="rdfs-seeAlso">mysql_connect()</a> - 打开一个到 MySQL 服务器的连接</span></li>
    <li class="member"><span class="function"><a href="mysql_pconnect.html" class="function" rel="rdfs-seeAlso">mysql_pconnect()</a> - 打开一个到 MySQL 服务器的持久连接</span></li>
    <li class="member"><span class="function"><a href="mysql_query.html" class="function" rel="rdfs-seeAlso">mysql_query()</a> - 发送一条 MySQL 查询</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>